package com.hht.toolanduilibs.util

import android.annotation.SuppressLint
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager
import android.content.Context
import android.net.ConnectivityManager
import android.net.wifi.WifiManager
import android.os.Build
import android.util.DisplayMetrics
import android.view.WindowManager
import androidx.annotation.RequiresApi
import java.lang.reflect.InvocationTargetException


/**
 * Author:chen'hu
 * Time:2023/8/7 11:15
 * Description:This is StateDetectionUtils
 */
object StateDetectionUtils {
    /**
     * 检测wifi连接状态
     * @param context
     * @return
     */
    fun isWifiConnecting(context: Context): Boolean {
        try {
            val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
            if (ni != null) {
                return ni.isConnected
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return false
    }

    /**
     * 获取当前连接的WiFi信号强度
     * @param context
     * @return
     */
    fun getWifiSignalLevel(context: Context): Int {
        val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
        //信号分几级
        val numberOfLevels = 4
        val wifiInfo = wifiManager.connectionInfo
        return WifiManager.calculateSignalLevel(wifiInfo.rssi, numberOfLevels)
    }

    /**
     * 获取蓝牙开关状态
     * @param context
     * @return
     */
    @RequiresApi(Build.VERSION_CODES.ECLAIR)
    fun getBluetoothState(context: Context): Boolean {
        var mBluetoothAdapter: BluetoothAdapter? = null
        try {
            mBluetoothAdapter = if (Build.VERSION.SDK_INT >= 18) {
                val manager =
                    context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
                manager.adapter
            } else {
                BluetoothAdapter.getDefaultAdapter()
            }
        } catch (t: Throwable) {
            t.printStackTrace()
        }
        if (mBluetoothAdapter == null) {
            return false
        }
        return mBluetoothAdapter.isEnabled
    }

    /**
     * 检测wifi热点状态
     * @param context
     * @return
     */
    fun getHotSpotState(context: Context): Boolean {
        try {
            val manager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
            //通过反射获取 getWifiApState()方法
            val method = manager.javaClass.getDeclaredMethod("getWifiApState")
            //调用getWifiApState() ，获取返回值
            val state = method.invoke(manager) as Int
            //通过反射获取 WIFI_AP的开启状态属性
            val field = manager.javaClass.getDeclaredField("WIFI_AP_STATE_ENABLED")
            //获取属性值
            val value = field[manager] as Int
            //判断是否开启
            return state == value
        } catch (e: NoSuchMethodException) {
            e.printStackTrace()
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
        } catch (e: InvocationTargetException) {
            e.printStackTrace()
        } catch (e: NoSuchFieldException) {
            e.printStackTrace()
        }
        return false
    }

    /**
     * 检测有线网连接状态
     * @param context
     * @return
     */
    fun isEthernetConnecting(context: Context): Boolean {
        try {
            val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val ni = cm.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET)
            if (ni != null) {
                return ni.isConnected
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return false
    }

    /**
     * 检测当的网络（WLAN、3G/2G）状态
     * @param context Context
     * @return true 表示网络可用
     */
    fun isNetworkConnected(context: Context): Boolean {
        val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val ni = cm.activeNetworkInfo
        return ni != null && ni.isConnectedOrConnecting
    }

    /**
     * 检测U盘连接状态
     */
    @SuppressLint("NewApi")
    fun isUsbMount(context: Context): Boolean {
        return StorageManagerUtils.newInstance(context).getUsbList().isNotEmpty()
    }

    fun isHaveNotification(context: Context, property: String): Boolean {
        val result: String = SystemPropertyUtil[context, property]
        return "1" == result
    }

    /**
     * 获取屏幕宽度
     */
    fun getScreenWidth(context: Context): Int {
        val wm = context
            .getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val outMetrics = DisplayMetrics()
        wm.defaultDisplay.getMetrics(outMetrics)
        return outMetrics.widthPixels
    }

    /**
     * 获取屏幕高度
     */
    fun getScreenHeight(context: Context): Int {
        val wm = context
            .getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val outMetrics = DisplayMetrics()
        wm.defaultDisplay.getMetrics(outMetrics)
        return outMetrics.heightPixels
    }

}